package com.dean.entity;

import cn.hutool.core.util.StrUtil;

import java.util.Map;

/**
 * @ClassName TableField
 * @Description 表字段信息
 * @Author dean
 * @Date 2021/10/3 10:03
 */
public class TableField {


	/**
	 * 列名 xxx_xxx / XXX_XXX
	 */
	private String columnName;

	/**
	 * 实体类字段名  xxxXxx
	 */
	private String fieldName;

	/**
	 * 首字母大写 字段名 XxxXxx
	 */
	private String firstUpperFieldName;
	/**
	 * 字段描述
	 */
	private String columnComment;
	/**
	 * 字段类型 int String ...
	 */
	private String fieldType;
	/**
	 * 数据类型
	 */
	private String dataType;
	/**
	 * 数据长度
	 */
	private Long dataLength;
	/**
	 * 小数点
	 */
	private Integer dataScale;

	private String pkFlag;


	private final String COLUMN_NAME = "COLUMN_NAME";
//	private final String FIELD_NAME = "FIELD_NAME";
//	private final String FIRST_UPPER_FIELD_NAME = "FIRST_UPPER_FIELD_NAME";
	private final String DATA_TYPE = "DATA_TYPE";
	private final String COLUMN_COMMENT = "COLUMN_COMMENT";
	private final String DATA_LENGTH = "DATA_LENGTH";
	private final String DATA_SCALE = "DATA_SCALE";
	private final String FIELD_TYPE = "FIELD_TYPE";

	private final String PK_FLAG = "PK_FLAG";


	public void TableField(){}
	public void TableField(Map<String,String> fieldMap){
		set(fieldMap);
	}
	/**
	 * 设置指定的字段信息
	 * @param fieldName
	 * @param fieldValue
	 */
	public void set(String fieldName,String fieldValue){
		fieldName = fieldName.toUpperCase();
		switch (fieldName){
			case COLUMN_NAME:
				setColumnName(fieldValue);
				break;
//			case FIELD_NAME:
//				setFieldName(fieldValue);
//				break;
//			case FIRST_UPPER_FIELD_NAME:
//				setFirstUpperFieldName(fieldValue);
//				break;
			case DATA_TYPE:
				setDataType(fieldValue);
				break;
			case COLUMN_COMMENT:
				setColumnComment(fieldValue);
				break;
			case DATA_LENGTH:
				setDataLength(fieldValue==null?null:Long.parseLong(fieldValue));
				break;
			case DATA_SCALE:
				setDataScale(fieldValue==null?null:Integer.parseInt(fieldValue));
				break;
			case FIELD_TYPE:
				setFieldType(fieldValue);
				break;
			case PK_FLAG:
				setPkFlag(fieldValue);
			default:
				break;
		}

	}

	public void set(Map<String,String> fieldMap){
		fieldMap.forEach((key,val)->{
			set(key.toUpperCase(),val);
		});
	}

	public String getColumnName() {
		return columnName;
	}

	public void setColumnName(String columnName) {
		this.columnName = columnName;
		this.fieldName = StrUtil.toCamelCase(this.columnName.toLowerCase());
		this.firstUpperFieldName = StrUtil.upperFirst(this.fieldName);
	}

	public String getFieldName() {
		return fieldName;
	}

//	public void setFieldName(String fieldName) {
//		this.fieldName = fieldName;
//	}

	public String getFirstUpperFieldName() {
		return firstUpperFieldName;
	}

//	public void setFirstUpperFieldName(String firstUpperFieldName) {
//		this.firstUpperFieldName = firstUpperFieldName;
//	}

	public String getDataType() {
		return dataType;
	}

	public void setDataType(String dataType) {
		this.dataType = dataType;
	}

	public String getColumnComment() {
		return columnComment;
	}

	public void setColumnComment(String columnComment) {
		this.columnComment = columnComment;
	}

	public Long getDataLength() {
		return dataLength;
	}

	public void setDataLength(Long dataLength) {
		this.dataLength = dataLength;
	}

	public Integer getDataScale() {
		return dataScale;
	}

	public void setDataScale(Integer dataScale) {
		this.dataScale = dataScale;
	}

	public String getFieldType() {
		return fieldType;
	}

	public void setFieldType(String fieldType) {
		this.fieldType = fieldType;
	}

	public String getPkFlag() {
		return pkFlag;
	}

	public void setPkFlag(String pkFlag) {
		this.pkFlag = pkFlag;
	}

	@Override
	public String toString() {
		return "TableField{" +
				"columnName='" + columnName + '\'' +
				", fieldName='" + fieldName + '\'' +
				", firstUpperFieldName='" + firstUpperFieldName + '\'' +
				", columnComment='" + columnComment + '\'' +
				", fieldType='" + fieldType + '\'' +
				", dataType='" + dataType + '\'' +
				", dataLength=" + dataLength +
				", dataScale=" + dataScale +
				", pkFlag='" + pkFlag + '\'' +
				'}';
	}
}
